package com.shzu.jwc.ty.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shzu.jwc.ty.bean.SystemStudytime;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Page;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.Studytimeservice;

@Controller
@RequestMapping("studytime")
public class StudyTimecontroller {

	@Autowired
	Studytimeservice studytimeservice;
	
	@RequestMapping("/studytime.action")
	public String getstudytime(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(studytimeservice.countstudytime(), pn);
		page.setPageSize(20);
		List<SystemStudytime> data=studytimeservice.getStudytime(page);
		model.addAttribute("data", data);
		model.addAttribute("nowyear",Semester.getnow());
		model.addAttribute("page", page);
		return "studytime/studytime";
	}
	
	@RequestMapping("/searchstudytime.action")
	public String searchstudytime(@RequestParam("userStudentInfoName")String userStudentInfoName,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		List<SystemStudytime> data=studytimeservice.searchstudytime(userStudentInfoName);
		model.addAttribute("data", data);
		model.addAttribute("nowyear",Semester.getnow());
		return "studytime/studytime";
	}
	
	@ResponseBody
	@RequestMapping(value="/upload.action",method=RequestMethod.POST)
	public Msg upload(@RequestParam("file") MultipartFile file) {
		  if(file.isEmpty())
			  return Msg.error("文件不能为空");
		  
		  String name=file.getOriginalFilename();
		  if(!name.endsWith(".xlsx")) {
			  return Msg.error("文件类型必须为xlsx");
		  }
		  try {
			  studytimeservice.batchImport(name, file);
		  return Msg.success("导入成功");
		  }catch(Exception ex) {
			  return Msg.error("导入失败,"+ex.getMessage());
		  }
	}
}
